package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.DelReq;
import com.cloud.lan.api.dto.req.LanGoodsLocationsReq;
import com.cloud.lan.dao.LanGoodsLocationsMapper;
import com.cloud.lan.dao.LanStoreMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.exceptions.BaseException;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanGoodsLocationsDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanGoodsLocationsService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;
import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 商品购买信息 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-05-04
 */
@Slf4j
@Service
public class LanGoodsLocationsServiceImpl extends ServiceImpl<LanGoodsLocationsMapper, LanGoodsLocationsDO> implements ILanGoodsLocationsService {

    @Resource
    LanGoodsLocationsMapper lanGoodsLocationsMapper;
    @Resource
    LanStoreMapper lanStoreMapper;

    @Override
    public BaseResponse manageList(LanGoodsLocationsReq req) {
        log.info("#查询 商品购买信息 列表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getGoodsId())},
                new String[]{"goodsId"}, LanError.ILLEGAL_PARAMS.getCode());
        LambdaQueryWrapper<LanGoodsLocationsDO> qw = new LambdaQueryWrapper<>();
        qw.eq(LanGoodsLocationsDO::getIsDeleted, false).orderByDesc(LanGoodsLocationsDO::getCreateTime);
        qw.eq(LanGoodsLocationsDO::getGoodsId, req.getGoodsId());
        Page<LanGoodsLocationsDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<LanGoodsLocationsDO> pageList = lanGoodsLocationsMapper.selectPage(page, qw);
        pageList.getRecords().forEach(r -> r.setLanStoreDO(lanStoreMapper.selectById(r.getStoreId())));
        return BaseResponse.success(pageList);
    }

    @Override
    public BaseResponse add(LanGoodsLocationsReq req) {
        log.info("#新增 商品购买信息 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getGoodsId()),
                        String.valueOf(req.getStoreId()), req.getLocations()},
                new String[]{"goodsId", "storeId", "locations"}, LanError.ILLEGAL_PARAMS.getCode());
        LanGoodsLocationsDO lanGoodsLocationsDO = new LanGoodsLocationsDO();
        BeanUtil.copyProperties(req, lanGoodsLocationsDO);
        LambdaQueryWrapper<LanGoodsLocationsDO> luwQw = new LambdaQueryWrapper<>();
        luwQw.eq(LanGoodsLocationsDO::getGoodsId, lanGoodsLocationsDO.getGoodsId());
        luwQw.eq(LanGoodsLocationsDO::getStoreId, lanGoodsLocationsDO.getStoreId());
        luwQw.eq(LanGoodsLocationsDO::getIsDeleted, false);
        if (lanGoodsLocationsMapper.selectCount(luwQw) > ZERO.getKey()) {
            log.error("{}", LanError.LAN_050003.getMsg());
            throw new BaseException(LanError.LAN_050003);
        }
        lanGoodsLocationsDO.setGoodsNo(RandomUtil.randomNumbers(4));
        lanGoodsLocationsMapper.insert(lanGoodsLocationsDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse update(LanGoodsLocationsReq req) {
        log.info("#更新 商品购买信息 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId()), String.valueOf(req.getGoodsId()),
                        String.valueOf(req.getStoreId()), req.getLocations()},
                new String[]{"id", "goodsId", "storeId", "locations"}, LanError.ILLEGAL_PARAMS.getCode());
        LanGoodsLocationsDO lanGoodsLocationsDO = lanGoodsLocationsMapper.selectById(req.getId());
        BeanUtil.copyProperties(req, lanGoodsLocationsDO);
        LambdaQueryWrapper<LanGoodsLocationsDO> luwQw = new LambdaQueryWrapper<>();
        luwQw.eq(LanGoodsLocationsDO::getGoodsId, lanGoodsLocationsDO.getGoodsId());
        luwQw.eq(LanGoodsLocationsDO::getStoreId, lanGoodsLocationsDO.getStoreId());
        luwQw.eq(LanGoodsLocationsDO::getIsDeleted, false);
        if (lanGoodsLocationsMapper.selectCount(luwQw) > ZERO.getKey()) {
            log.error("{}", LanError.LAN_050003.getMsg());
            throw new BaseException(LanError.LAN_050003);
        }
        lanGoodsLocationsMapper.updateById(lanGoodsLocationsDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse delete(DelReq req) {
        log.info("#删除 商品购买信息 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())}, new String[]{"id"}, LanError.ILLEGAL_PARAMS.getCode());
        LambdaUpdateWrapper<LanGoodsLocationsDO> luw = new LambdaUpdateWrapper<>();
        luw.set(LanGoodsLocationsDO::getIsDeleted, ONE.getKey()).eq(LanGoodsLocationsDO::getId, req.getId());
        lanGoodsLocationsMapper.update(new LanGoodsLocationsDO(), luw);
        return BaseResponse.success();
    }

}
